package com.lvliucun.validate.check.impl;

import com.alibaba.fastjson.JSONObject;
import com.lvliucun.validate.check.ParamChecker;
import com.lvliucun.validate.exception.CustomerErrorCodeEnum;
import com.lvliucun.validate.exception.ParamException;
import com.lvliucun.validate.utils.FiledNameSplitUtil;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.util.Map;
import java.util.regex.Pattern;

/**
 * @version 1.0
 * @Description 参数校验器：手机号
 * @Author:zhuojiajin
 * @Date 2020/8/14
 **/
@Component
public class ParamMobile implements ParamChecker {

    /**
     * @Description 参数非空校验器
     * @Author zhuojiajin
     * @Param [args, filedName]
     * @Return boolean
     * @Date 2020年8月14日14:46:50
     **/
    @Override
    public boolean checkParam(JSONObject args, String filedName,String path) throws Exception {
        String[] filedStr = FiledNameSplitUtil.getFileName(filedName);
        Map<String,Object> value = FiledNameSplitUtil.getFiledValue(filedStr,args);
        if(null == value || StringUtils.isEmpty(value.get("value"))){
            //抛出自定义异常
            throw new ParamException(CustomerErrorCodeEnum.PARAM_NOT_NULL,"手机号",path);
        }else{
            String regex = "(\\+\\d+)?1[3456789]\\d{9}$";
            boolean result = Pattern.matches(regex, value.get("value").toString());
            if(!result){
                //抛出自定义异常
                throw new ParamException(CustomerErrorCodeEnum.PARAM_ILLEGAL,"手机号",path);
            }else{
                return result;
            }
        }
    }
}

